package com.shuai.controller;

import com.shuai.constant.JwtClaimsConstant;
import com.shuai.constant.LoginTypeConstant;
import com.shuai.dto.PasswordEditDTO;
import com.shuai.dto.EnterpriseDTO;
import com.shuai.dto.EnterpriseLoginDTO;
import com.shuai.entity.Enterprise;
import com.shuai.properties.JwtProperties;
import com.shuai.result.Result;
import com.shuai.service.EnterpriseService;
import com.shuai.service.UserService;
import com.shuai.utils.JwtUtil;
import com.shuai.vo.EnterpriseLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/enterprise")
@Slf4j
@Api(tags = "企业相关接口")
public class EnterpriseController {
    @Autowired
    EnterpriseService enterpriseService;

    @Autowired
    UserService userService;

    @Autowired
    JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param enterpriseLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "企业登录")
    public Result<EnterpriseLoginVO> login(@RequestBody EnterpriseLoginDTO enterpriseLoginDTO) {
        log.info("企业登录：{}", enterpriseLoginDTO);
        Enterprise enterprise = enterpriseService.login(enterpriseLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ID, enterprise.getId());
        claims.put(JwtClaimsConstant.LOGIN_TYPE, LoginTypeConstant.ENTERPRISE_LOGIN);
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        EnterpriseLoginVO enterpriseLoginVO = EnterpriseLoginVO.builder()
                .id(enterprise.getId())
                .enterpriseName(enterprise.getEnterpriseName())
                .logo(enterprise.getLogo())
                .token(token)
                .build();

        return Result.success(enterpriseLoginVO);
    }

    /**
     * 新增企业(注册)
     * @Param enterpriseDTO
     * @return Result
     * */
    @PostMapping("/register")
    @ApiOperation("新增企业")
    public Result save(@RequestBody EnterpriseDTO enterpriseDTO){
        log.info("新增企业，{}",enterpriseDTO);
        enterpriseService.save(enterpriseDTO);
        return Result.success();
    }

    /**
     *根据id查询企业信息
     * */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询企业信息")
    public Result<Enterprise> getById(@PathVariable Long id){
        log.info("根据id查询企业信息：{}",id);
        Enterprise enterprise = enterpriseService.getById(id);
        return Result.success(enterprise);
    }

    /**
     * 修改企业信息
     * */
    @PutMapping()
    @ApiOperation("修改企业信息")
    public Result update(@RequestBody EnterpriseDTO enterpriseDTO) {
        log.info("修改企业信息：{}", enterpriseDTO);
        enterpriseService.update(enterpriseDTO);
        return Result.success();
    }

    /**
     * 修改密码
     * */
    @PutMapping("/setPassword")
    @ApiOperation("修改密码")
    public Result updatePassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("修改密码：{}",passwordEditDTO);
        enterpriseService.updatePassword(passwordEditDTO);
        return Result.success();
    }

}